home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Compilers⁄Interps / Harvest C / Application / Examples / Harvest MiniEdit / mini.windows.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-03-17  |  5.1 KB  |  254 lines  |  [TEXT/KAHL]

  1. /*********************************************************************
  2.  
  3.     mini.windows.c
  4.     
  5.     window functions for MiniEdit
  6.     
  7. *********************************************************************/
  8.  
  9. #include <Windows.h>
  10. #include <Controls.h>
  11. #include <TextEdit.h>
  12. #include <ToolUtils.h>
  13. #include <Fonts.h>
  14. #include "MiniEdit.h"
  15. #include "mini.windows.h"
  16. #include "mini.file.h"
  17.  
  18. extern WindowRecord     wRecord;
  19. extern WindowPtr     myWindow;
  20. extern ControlHandle vScroll;
  21. extern TEHandle      TEH;
  22. extern char             dirty;
  23. extern Str255          theFileName;
  24. extern int             linesInFolder;
  25.  
  26.     /**
  27.      **        Prototypes for private functions.
  28.      **        (They really should be static.)
  29.      **
  30.      **/
  31.  
  32. int AdjustText (void);
  33. int SetVScroll(void);
  34. int SetView (WindowPtr w);
  35. pascal void ScrollProc (ControlHandle theControl, int theCode);
  36.  
  37.  
  38.  
  39. int SetUpWindows(void)
  40. {
  41.     Rect    destRect, viewRect;
  42.     Rect    vScrollRect;
  43.     FontInfo    myInfo;
  44.     int        height;
  45.     
  46.     SetPort((myWindow = GetNewWindow(windowID, &wRecord, (WindowPtr) -1L)));
  47.     TextFont(monaco);
  48.     TextSize(9);
  49.     vScrollRect = (*myWindow).portRect;
  50.     vScrollRect.left = vScrollRect.right-15;
  51.     vScrollRect.right += 1;
  52.     vScrollRect.bottom -= 14;
  53.     vScrollRect.top -= 1;
  54.     vScroll = NewControl( myWindow, &vScrollRect, "\p", 1, 0, 0, 0,
  55.         scrollBarProc, 0L);
  56.  
  57.     viewRect = qd.thePort->portRect;
  58.     viewRect.right -= SBarWidth;
  59.     viewRect.bottom -= SBarWidth;
  60.     InsetRect(&viewRect, 4, 4);
  61.     TEH = TENew(&viewRect, &viewRect);
  62.     SetView(qd.thePort);
  63.     dirty = 0;
  64. }
  65.  
  66.  
  67. int AdjustText (void)
  68.  
  69. {
  70.     int        oldScroll, newScroll, delta;
  71.     
  72.     oldScroll = (**TEH).viewRect.top - (**TEH).destRect.top;
  73.     newScroll = GetCtlValue(vScroll) * (**TEH).lineHeight;
  74.     delta = oldScroll - newScroll;
  75.     if (delta != 0)
  76.       TEScroll(0, delta, TEH);
  77. }
  78.  
  79.  
  80. int SetVScroll(void)
  81. {
  82.     register int    n;
  83.     
  84.     n = (**TEH).nLines-linesInFolder;
  85.  
  86.     if ((**TEH).teLength > 0 && (*((**TEH).hText))[(**TEH).teLength-1]=='\r')
  87.         n++;
  88.  
  89.     SetCtlMax(vScroll, n > 0 ? n : 0);
  90. }
  91.  
  92. int ShowSelect (void)
  93.  
  94. {
  95.     register    int        topLine, bottomLine, theLine;
  96.     
  97.     SetVScroll();
  98.     AdjustText();
  99.     
  100.     topLine = GetCtlValue(vScroll);
  101.     bottomLine = topLine + linesInFolder;
  102.     
  103.     if ((**TEH).selStart < (**TEH).lineStarts[topLine] ||
  104.             (**TEH).selStart >= (**TEH).lineStarts[bottomLine]) {
  105.         for (theLine = 0; (**TEH).selStart >= (**TEH).lineStarts[theLine]; theLine++)
  106.             ;
  107.         SetCtlValue(vScroll, theLine - linesInFolder / 2);
  108.         AdjustText();
  109.     }
  110. }
  111.  
  112. int SetView (WindowPtr w)
  113.  
  114. {
  115.     (**TEH).viewRect = w->portRect;
  116.     (**TEH).viewRect.right -= SBarWidth;
  117.     (**TEH).viewRect.bottom -= SBarWidth;
  118.     InsetRect(&(**TEH).viewRect, 4, 4);
  119.  
  120.     linesInFolder = ((**TEH).viewRect.bottom-(**TEH).viewRect.top)/(**TEH).lineHeight;
  121.     (**TEH).viewRect.bottom = (**TEH).viewRect.top + (**TEH).lineHeight*linesInFolder;
  122.     (**TEH).destRect.right = (**TEH).viewRect.right;
  123.     TECalText(TEH);
  124. }
  125.  
  126. int UpdateWindow (WindowPtr theWindow)
  127.  
  128. {
  129.     GrafPtr    savePort;
  130.     
  131.     GetPort(&savePort);
  132.     SetPort(theWindow);
  133.  
  134.     BeginUpdate(theWindow);
  135.     EraseRect(&theWindow->portRect);
  136.     DrawControls(theWindow);
  137.     DrawGrowIcon(theWindow);
  138.     TEUpdate(&theWindow->portRect, TEH);
  139.     EndUpdate(theWindow);
  140.  
  141.     SetPort(savePort);
  142. }
  143.  
  144.  
  145.  
  146.  
  147. pascal void ScrollProc (ControlHandle theControl, int theCode)
  148.  
  149. {
  150.     int    pageSize;
  151.     int    scrollAmt;
  152.     int oldCtl;
  153.     
  154.     if (theCode == 0)
  155.         return ;
  156.     
  157.     pageSize = ((**TEH).viewRect.bottom-(**TEH).viewRect.top) / 
  158.             (**TEH).lineHeight - 1;
  159.             
  160.     switch (theCode) {
  161.         case inUpButton: 
  162.             scrollAmt = -1;
  163.             break;
  164.         case inDownButton: 
  165.             scrollAmt = 1;
  166.             break;
  167.         case inPageUp: 
  168.             scrollAmt = -pageSize;
  169.             break;
  170.         case inPageDown: 
  171.             scrollAmt = pageSize;
  172.             break;
  173.     }
  174.  
  175.     oldCtl = GetCtlValue(theControl);
  176.     SetCtlValue(theControl, oldCtl+scrollAmt);
  177.  
  178.     AdjustText();
  179.  
  180. }
  181.  
  182. int DoContent(WindowPtr theWindow, EventRecord *theEvent)
  183.  
  184. {
  185.     int                cntlCode;
  186.     ControlHandle     theControl;
  187.     int                pageSize;
  188.     GrafPtr            savePort;
  189.     
  190.     GetPort(&savePort);
  191.     SetPort(theWindow);
  192.  
  193.     GlobalToLocal(&theEvent->where);
  194.     if ((cntlCode = FindControl(theEvent->where, theWindow, &theControl)) == 0) {
  195.         if (PtInRect(theEvent->where, &(**TEH).viewRect))
  196.             TEClick(theEvent->where, (theEvent->modifiers & shiftKey)!=0, TEH);
  197.     } else if (cntlCode == inThumb) {
  198.         TrackControl(theControl, theEvent->where, 0L);
  199.         AdjustText();
  200.     } else
  201.         TrackControl(theControl, theEvent->where, &ScrollProc);
  202.  
  203.     SetPort(savePort);
  204. }
  205.  
  206. int MyGrowWindow(WindowPtr w, Point p)
  207.  
  208. {
  209.     GrafPtr    savePort;
  210.     long    theResult;
  211.     Rect    oldHorizBar;
  212.     Rect     r;
  213.     
  214.     GetPort(&savePort);
  215.     SetPort(w);
  216.     
  217.     oldHorizBar = w->portRect;
  218.     oldHorizBar.top = oldHorizBar.bottom - (SBarWidth+1);
  219.  
  220.     SetRect(&r, 80, 80, qd.screenBits.bounds.right, qd.screenBits.bounds.bottom);
  221.     theResult = GrowWindow(w, p, &r);
  222.     if (theResult == 0)
  223.       return 0;
  224.     SizeWindow( w, LoWord(theResult), HiWord(theResult), false);
  225.  
  226.     InvalRect(&w->portRect);
  227.     
  228.     SetView(w);
  229.  
  230.     EraseRect(&oldHorizBar);
  231.     
  232.     MoveControl(vScroll, w->portRect.right - SBarWidth, w->portRect.top-1);
  233.     SizeControl(vScroll, SBarWidth+1, w->portRect.bottom - w->portRect.top-(SBarWidth-2));
  234.     r = (**vScroll).contrlRect;
  235.     ValidRect(&r);
  236.  
  237.  
  238.     SetVScroll();
  239.     AdjustText();
  240.     
  241.     SetPort(savePort);
  242. }
  243.  
  244.  
  245.  
  246. int CloseMyWindow(void)
  247. {
  248.     HideWindow(myWindow);
  249.     TESetSelect(0, (**TEH).teLength, TEH);
  250.     TEDelete(TEH);
  251.     SetVScroll();
  252.     SetUpFiles();
  253. }
  254.